package com.example.restatr.Atcivity.Traffic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Traffic.Bean.TrafficCheckCarPlaceBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CheckCarPlaceAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView list;
    private List<TrafficCheckCarPlaceBean.RowsBean> rowsBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_check_car_place_at );

        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        list = findViewById( R.id.list );
        Tools.SetBarTitle( tb, barTitle, "检车地点", search, this ,true);
        tb.getMenu().removeItem( R.id.item1 );
        tb.getMenu().getItem( 0 ).setTitle( "我的预约" );
        tb.setOnMenuItemClickListener( new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity( new Intent(CheckCarPlaceAt.this,CheckCarListAt.class) );
                return true;
            }
        } );
        GetCheckCarPlace();
    }

    private void GetCheckCarPlace() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/traffic/checkCar/place/list", MainActivity.sp.getString( "token", "" ) );
            TrafficCheckCarPlaceBean trafficCheckCarPlaceBean = new Gson().fromJson( token, TrafficCheckCarPlaceBean.class );
            rowsBeanList.addAll( trafficCheckCarPlaceBean.getRows() );
            if (rowsBeanList.size() > 0) {
                runOnUiThread( () -> {
                    list.setAdapter( new adapter() );
                } );
            }
        } ).start();
    }

    private void PostCheckCar(int pos) {
        new Thread( () -> {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put( "userId", MainActivity.sp.getInt( "userId", 0 ) );
                jsonObject.put( "aptTime", new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ).format( new Date() ) );
                jsonObject.put( "addressId", rowsBeanList.get( pos ).getId() );
                String token = Tools.Post( "/prod-api/api/traffic/checkCar/apply", MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                JSONObject jx = new JSONObject( token );
                String msg = jx.getString( "msg" );
                int code = jx.getInt( "code" );
                runOnUiThread( () -> {
                    Toast.makeText( this, msg, Toast.LENGTH_SHORT ).show();
                } );
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } ).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeanList.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate( R.layout.job_item_layout, null );
            TextView professionName = convertView.findViewById( R.id.professionName );
            TextView salary = convertView.findViewById( R.id.salary );
            TextView need = convertView.findViewById( R.id.need );
            TextView companyName = convertView.findViewById( R.id.companyName );
            TextView applyFor = convertView.findViewById( R.id.applyFor );

            professionName.setText( rowsBeanList.get( position ).getPlaceName() );
            salary.setText( rowsBeanList.get( position ).getAddress() );
            need.setText( "电话："+rowsBeanList.get( position ).getPhone() );
            companyName.setText(rowsBeanList.get( position ).getRemarks() );
            applyFor.setText( "预约" );
            applyFor.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PostCheckCar( position );
                }
            } );
            return convertView;
        }
    }
}